import wx
from RecordingPanel import RecordingPanel
from PlaybackPanel import PlaybackPanel

VISNOTEBOOK = 8938

##  Notebook tab containing the visualization for the recording and playing of measurements of movements.
class VisualizationPanel(wx.Panel):

    ##  The contructor of VisualizationPanel. GUI elements of the panel are setup here.
    #   @param[in]  parent  The wx window parent (notebook) of this panel.
    #   @param[in]  state   VisualizationState instance attached to this panel.
    def __init__(self, parent, state):
        wx.Panel.__init__(self, parent)
        ##  VisualizationState instance attached to this panel.
        self.__state = state
        self.__state.panel = self
        ##  The name of this notebook panel.        
        self.name = "Visualization"
        ##  The wx window parent (notebook) of this panel.
        self.parent = parent
        ##  The wx.Notebook().
        self.visNotebook = wx.Notebook(self, id=VISNOTEBOOK, style=0)

        ## Only load the recording page when the fob is running
        if self.__state.main.FOB_RUNNING:
            self.LoadRecordingPage()

        self.LoadPlaybackPage()

        self.SetSize((915, 760))

        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        sizer_1.Add(self.visNotebook, 1, wx.EXPAND, 0)
        self.SetSizer(sizer_1)

        self.Bind(wx.EVT_NOTEBOOK_PAGE_CHANGED, self.OnPageChanged, id=VISNOTEBOOK)

        self.Layout()
        self.Centre()

    ##  Loads the subpanel (referred to with "page") for recording and realtime visualization.
    def LoadRecordingPage(self):
        self.visNotebook.AddPage(RecordingPanel(self.visNotebook, self.__state.recordingState), "Recording")

    ##  Loads the subpanel for replaying and importing recordings.
    def LoadPlaybackPage(self):
        self.visNotebook.AddPage(PlaybackPanel(self.visNotebook, self.__state.playbackState), "Playback")

    ##  Panel navigation, goes back in the panel navigation to the previous state.
    #   @param[in]  new The tab index to go to.
    def OnBack(self, event, new = 2):
        self.__state.Stop()
        self.parent.ChangeSelection(new)

    ##  Sets the properties when the selection of the subpanels is switched.
    def OnPageChanged(self, event):

        if self.__state.playbackState.PLAYING:
            self.__state.playbackState.panel.OnTogglePlayPause(event)
        try:
            self.__state.recordingState.OnTogglePlayPause()
        except:
            msg = "Cannot switch panels when a recording is being played, please press pause first."
            dlg = wx.MessageDialog(self.__state.main.gui, msg, 'Warning', wx.OK | wx.NO_DEFAULT | wx.ICON_EXCLAMATION)
            dlg.ShowModal()
            self.parent.ChangeSelection(3)

        if self.__state.recordingState.FIRST_TIME or self.__state.playbackState.FIRST_TIME:
            self.__state.update_actor_positions()
